#!/usr/bin/env bash

# REQUIRES: preload
# RUN: bash %s %T/unkown_compiler_recognised
# RUN: cd %T/unkown_compiler_recognised; %{intercept-build} --use-cc=%T/unkown_compiler_recognised/wrapper --use-c++=%T/unkown_compiler_recognised/wrapper++ --cdb wrapper.json ./run.sh
# RUN: cd %T/unkown_compiler_recognised; %{cdb_diff} wrapper.json expected.json

set -o errexit
set -o nounset
set -o xtrace

# the test creates a subdirectory inside output dir.
#
# ${root_dir}
# ├── wrapper
# ├── wrapper++
# ├── run.sh
# ├── expected.json
# └── src
#    └── empty.c

root_dir=$1
mkdir -p "${root_dir}/src"

touch "${root_dir}/src/empty.c"

wrapper_file="${root_dir}/wrapper"
cat > ${wrapper_file} << EOF
#!/usr/bin/env bash

true
EOF
chmod +x ${wrapper_file}

wrapperxx_file="${root_dir}/wrapper++"
cat > ${wrapperxx_file} << EOF
#!/usr/bin/env bash

true
EOF
chmod +x ${wrapperxx_file}

build_file="${root_dir}/run.sh"
cat > ${build_file} << EOF
#!/usr/bin/env bash

set -o nounset
set -o xtrace

${wrapper_file} -c -Dver=1 src/empty.c;
${wrapperxx_file} -c -Dver=2 src/empty.c;

cd src
${wrapper_file} -c -Dver=3 empty.c;
${wrapperxx_file} -c -Dver=4 empty.c;
EOF
chmod +x ${build_file}

cat > "${root_dir}/expected.json" << EOF
[
{
  "command": "cc -c -Dver=1 src/empty.c",
  "directory": "${root_dir}",
  "file": "src/empty.c"
}
,
{
  "command": "c++ -c -Dver=2 src/empty.c",
  "directory": "${root_dir}",
  "file": "src/empty.c"
}
,
{
  "command": "cc -c -Dver=3 empty.c",
  "directory": "${root_dir}/src",
  "file": "empty.c"
}
,
{
  "command": "c++ -c -Dver=4 empty.c",
  "directory": "${root_dir}/src",
  "file": "empty.c"
}
]
EOF
